﻿using Dapper;
using SYJ.BLL;
using SYJ.Common.web;
using SYJ.IBLL;
using SYJ.Model;
using SYJ.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace SYJ.Logic.Controllers.admin
{
   public class ContestPictureMgrController:BaseController
    {
       IContestPictureBLL bll = new ContestPictureBLL();
       IContestBLL cbll = new ContestBLL();
        public ActionResult Index()
        {
            ViewBag.ContestList = cbll.GetAll();
            return View();
        }

        public ActionResult GetContestPictureList(QueryModel query, int? contest_id, int? status, int? grouping)
        {
            long count = 0;

            if (contest_id != null && contest_id > 0)
            {
                query.SqlWhere += " and a.contest_id = ?contest_id ";
                query.Param.Add("contest_id", contest_id);
            }
            if (status != null && status >= 0)
            {
                query.SqlWhere += " and a.status = ?status ";
                query.Param.Add("status", status);
            }
            if (grouping != null && grouping >= 0)
            {
                query.SqlWhere += " and a.grouping = ?grouping ";
                query.Param.Add("grouping", grouping);
            }
            List<ContestPicture> list = bll.GetList(query, out count);
            var respose = JsonLayuiGridData<ContestPicture>.Create(query, list, count, "");
            return Json(respose, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public ActionResult AddOrUp(int? id)
        {
            ContestPicture role = new ContestPicture();
            if (id != null)
            {
                var param = new DynamicParameters();
                param.Add("id", id);
                role = bll.GetModel("select a.*,b.nickname as create_author from sys_leaves a left join sys_user b on a.user_id=b.id where a.id=?id", param);
            }
            return View(role);
        }
        [HttpPost]
        public ActionResult AddOrUp(ContestPicture model)
        {
            if (model.id > 0)
            {
                ContestPicture leave = bll.GetModel(model.id);
                leave.status = model.status;
                return Json(bll.Update(leave), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(bll.Insert(model), JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult Delete(int id)
        {
            bool delete = bll.Delete(new ContestPicture() { id = id });
            return Json(delete, JsonRequestBehavior.AllowGet);
        }
    }
}
